﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Gencode.PrsCommon.Data.Person;
using Gencode.PrsCommon.App;
using Gencode.Gengui.Forms;

namespace Personalplaner.Gui.Mask
{
    [ToolboxItem(false)]
	public partial class PersonalStamm : UserControl, Gencode.Gengui.Forms.ISaveNewDialog
	{
		Gencode.PrsCommon.Data.Person.Person person;

		public PersonalStamm()
		{
			InitializeComponent();
			GridPersonen.InitData(Verwaltung.Mandant, PersonTyp.Stammperson);
			comboAnstellung.InitData(Verwaltung.Mandant);
		}

		~PersonalStamm()
		{
			person = null;
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		private void SetPersonenData()
		{
            if (person == null)
                UpdatePersonObject();

			if (person != null)
			{
				txtVorname.Text = person.Vorname;
				txtName.Text = person.Nachname;
				txtStrasse.Text = person.Strasse;
				txtStrNr.Text = person.StrassenNummer;
				txtOrt.Text = person.Ort;
				txtPlz.Text = person.PLZ;

				txtPostfach.Text = person.Postfach;
				txtEmail.Text = person.Email;

				txtTelN.Text = person.Natel;
				txtTelP.Text = person.TelefonPrivat;

				listAbteilung.InitData(person.PrsID, Verwaltung.Mandant, Gencode.PrsUtil.Controls.PUAbteilungListViewType.Assigned);
				comboAnstellung.SetIndexAt(person.AnstellungID);

				DataView dv = PersonData.GetGesamtPensum(person.PrsID);
                if (dv.Count > 0)
                {
                    numPens.Value = int.Parse(dv[0].Row["pensum"].ToString());
                    pensVon.Text = DateUtil.ConvertSqlToDate(dv[0].Row["von"].ToString());
                    pensBis.Text = DateUtil.ConvertSqlToDate(dv[0].Row["bis"].ToString());

                }
                else
                {
                    numPens.Value = 0;
                    pensVon.Text = DateUtil.ConvertSqlToDate("");
                    pensBis.Text = DateUtil.ConvertSqlToDate("");
                }
				dv.Dispose();
			}
		}

		#region Interface Members

		public void NewClicked()
		{
			if (GridPersonen.Rows.Count > 0)
			{
				GridPersonen[0, 0].Selected = true;
				GridPersonen[0, 0].Selected = false;
			}

			var addprs = new Gencode.PrsUtil.Dialog.Stammdaten.PersonWizard.AddPersonWizard(Verwaltung.Mandant);
			addprs.Show(this);

			//Form dialog = new AddPerson();
			//dialog.ShowDialog(this);

			GridPersonen.ReloadData();
		}

		public void SaveClicked()
		{
			if (GridPersonen.SelectedCells != null
				&& GridPersonen.SelectedCells.Count > 0
				&& GridPersonen.SelectedCells[0].RowIndex >= 0)
			{
				if (CheckTextFields())
				{
					//update person
					PersonData.UpdatePerson(GridPersonen.SelectedPrsID,
						txtVorname.Text, txtName.Text, txtStrasse.Text, txtStrNr.Text, txtPostfach.Text, txtPlz.Text, txtOrt.Text,
						txtTelP.Text, txtTelN.Text, txtEmail.Text, pensVon.Text, pensBis.Text, (int)numPens.Value,
						comboAnstellung.SelectedAnstellungID);

					GridPersonen.InitData(Verwaltung.Mandant, PersonTyp.Stammperson);
                    SetPersonenData();
				}
			}
			else
			{
				if (CheckTextFields())
				{
					//save new person
					PersonData.SavePerson(txtVorname.Text, txtVorname.Text, txtStrasse.Text, txtStrNr.Text, txtPostfach.Text, txtPlz.Text, txtOrt.Text,
						txtTelP.Text, txtTelN.Text, txtEmail.Text, pensVon.Text, pensBis.Text, (int)numPens.Value, comboAnstellung.SelectedAnstellungID, PersonTyp.Stammperson, Verwaltung.Mandant);

					GridPersonen.InitData(Verwaltung.Mandant, PersonTyp.Stammperson);
                    SetPersonenData();
				}
			}
		}

		private bool CheckTextFields()
		{
			bool check = true;
			if (txtName.Text == "")
			{
				check = false;
				txtName.BackColor = Color.Yellow;
			}
			if (txtVorname.Text == "")
			{
				check = false;
				txtVorname.BackColor = Color.Yellow;
			}
			if (txtStrasse.Text == "")
			{
				check = false;
				txtStrasse.BackColor = Color.Yellow;
			}

			if (txtPlz.Text == "")
			{
				check = false;
				txtPlz.BackColor = Color.Yellow;
			}
			if (txtOrt.Text == "")
			{
				check = false;
				txtOrt.BackColor = Color.Yellow;
			}

			return check;
		}

		public bool OKClicked()
		{
			//do nothing
			//just close dialog
			return true;
		}

		public bool CancleClicked()
		{
			//do nothing
			//just close dialog
			return true;
		}

		#endregion

		private void GridPersonen_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			person = new Gencode.PrsCommon.Data.Person.PersonAbteilung(GridPersonen.SelectedPrsID);
			SetPersonenData();

            tabPrsStamm.Enabled = true;
		}

		private void btnPensum_Click(object sender, EventArgs e)
		{
			if (GridPersonen.SelectedCells != null && GridPersonen.SelectedCells.Count > 0 && GridPersonen.SelectedCells[0].RowIndex >= 0)
			{
				int tmpIndex = GridPersonen.SelectedCells[0].RowIndex;
				GUDialogBox dlg = new GUDialogBox(new ArbeitspensumDlg(GridPersonen.SelectedPrsID), "Arbeitspensum");
				if(dlg.ShowDialog() == DialogResult.OK)
				{
                    person = null;
                    GridPersonen.ReloadData();
                    //GridPersonen[0, 0].Selected = true;
					SetPersonenData();
				}
			}
		}

		private void btnAnstellung_Click(object sender, EventArgs e)
		{
			if(GridPersonen.SelectedCells.Count > 0)
				if (GridPersonen.SelectedCells != null && GridPersonen.SelectedCells[0].RowIndex >= 0)
				{
					GUDialogBox dlg = new GUDialogBox(new AnstellungDlg(GridPersonen.SelectedPrsID, comboAnstellung.SelectedAnstellungID), "Anstellungen");
					if (dlg.ShowDialog() == DialogResult.OK)
					{
						comboAnstellung.SetIndexAt(person.AnstellungID);
					}
				}
		}

		private void btnAbteilung_Click(object sender, EventArgs e)
		{
			if (GridPersonen.SelectedCells != null && GridPersonen.SelectedCells.Count > 0 && GridPersonen.SelectedCells[0].RowIndex >= 0)
			{
				int tmpIndex = GridPersonen.SelectedCells[0].RowIndex;

				GUDialogBox dlg = new GUDialogBox(new PersonSetAbteilung(GridPersonen.SelectedPrsID), "Abteilungen");
				dlg.ShowDialog();

				GridPersonen.ReloadData();
				GridPersonen[0, tmpIndex].Selected = true;
				SetPersonenData();
			}
		}

		private void PersonalStamm_Load(object sender, EventArgs e)
		{
			if (GridPersonen.Rows.Count > 0)
			{
				GridPersonen[0, 0].Selected = true;

                GridPersonen[0, 0].Selected = false;
                
                //person = new Gencode.PrsCommon.Data.Person.PersonAbteilung(GridPersonen.SelectedPrsID);
                //SetPersonenData();

			}
		}

        private void checkAll_CheckedChanged(object sender, EventArgs e)
        {
            if (checkAll.Checked)
                GridPersonen.InitData(Verwaltung.Mandant, PersonTyp.Alle);
            else
                GridPersonen.InitData(Verwaltung.Mandant, PersonTyp.Stammperson);

            SetPersonenData();
        }

        private void GridPersonen_SelectionChanged(object sender, EventArgs e)
        {
            UpdatePersonObject();
            SetPersonenData();

            tabPrsStamm.Enabled = true;
        }

        private void UpdatePersonObject()
        {
            try
            {
                person = new Gencode.PrsCommon.Data.Person.PersonAbteilung(GridPersonen.SelectedPrsID);
            }catch(Exception)
            {
                person = null;
            }
        }
	}
}
